#include "StdAfx.h"
#include "UIPad.h"

CUIPad::CUIPad(void)
{
	m_nHovered = -1;
	m_nScrollPos = 0;
}

CUIPad::~CUIPad(void)
{
}

BOOL CUIPad::Create(CString szBmpFile, CRect rcBound, CUIControl *pParent)
{
	m_imgBk.Load(szBmpFile);
    CUIControl::CreateUI(rcBound, pParent);
	return TRUE;
}

void CUIPad::AddItem(PadItem &item)
{
    m_listItem.AddTail(item);
	PadItem &tail = m_listItem.GetTail();

	CString szItemFile;
	szItemFile.Format("pad\\%d.png",item.dwGameId);
	szItemFile = CResourceManager::GetFullPath(szItemFile);
	tail.imgItem.Load(szItemFile);
}

void CUIPad::SetBound(CRect rcBound)
{
	CUIControl::SetBound(rcBound);
	int nWidth = rcBound.Width();
	int nHeight = rcBound.Height();
	int nCount = m_listItem.GetCount();
	int nColCount = nWidth / PadItem_Large_Width;
	int nRowCount = nColCount == 0 ? 0 : (((nCount % nColCount) == 0) ? (nCount / nColCount) : (nCount / nColCount + 1));
	int nMargin = (nWidth - nColCount * PadItem_Large_Width) / 2;

	for(int row = 0; row < nRowCount; row++)
	{
		for(int col = 0; col < nColCount; col++)
		{
			int i = row * nColCount + col;

			if(i >= nCount) 
				break;
			PadItem & item = m_listItem.GetAt(i);

			int nLeft = nMargin + col * PadItem_Large_Width;
			int nTop = row * PadItem_Large_Height;
			item.rcItem = CRect(nLeft, nTop, nLeft + PadItem_Large_Width, nTop + PadItem_Large_Height);
		}
	} 
}

void CUIPad::Render(CDC *pDC)
{
	CRect rcBound = CRect(0, 0, m_rcBound.Width(), m_rcBound.Height());
    Child2Window(rcBound);

	if(m_imgBk.IsValid())
	{
	    m_imgBk.Draw(pDC->m_hDC, rcBound);
	}

	int nCount = m_listItem.GetCount();
	for(int i = 0; i < nCount; i++)
	{
		PadItem & item = m_listItem.GetAt(i);
		RenderItem(pDC, item, (m_nHovered==i));
	} 
}

void CUIPad::RenderItem(CDC *pDC, PadItem &item, BOOL bHover)
{
    if(item.imgItem.IsValid())
	{
		int nImgWidth = item.imgItem.GetWidth() / 3;
        int nImgHeight = item.imgItem.GetHeight();

		CRect rcDst, rcSrc;
		if(bHover)
		{
            rcDst = item.rcItem;
			rcSrc = CRect(nImgWidth, 0, nImgWidth * 2, nImgHeight);
		}
		else
		{
			int nLeft = item.rcItem.left + (item.rcItem.Width() - PadItem_Normal_Width) / 2;
			int nTop = item.rcItem.top + (item.rcItem.Height() - PadItem_Normal_Height) / 2;
			rcDst = CRect(nLeft, nTop, nLeft + PadItem_Normal_Width, nTop + PadItem_Normal_Height);
			rcSrc = CRect(0, 0, nImgWidth, nImgHeight);
	    }
		
		Child2Window(rcDst);
        item.imgItem.DrawPart(pDC->m_hDC, rcDst, rcSrc);
	}
}

BOOL CUIPad::OnLButtonDown(UINT nFlags, CPoint point)
{
	int nCount = m_listItem.GetCount();
	for(int i = 0; i < nCount; i++)
	{
		PadItem &item = m_listItem.GetAt(i);

		if(item.rcItem.PtInRect(point))
		{
			UIAction action;
			action.dwAction = Pad_Item_Select;
			action.dwParam = item.dwGameId;
			action.pUISource = this;
			m_pContainer->PerformAction(action);
		}
	}
	return TRUE;
}

BOOL CUIPad::OnMouseOver(UINT nFlags, CPoint point)
{
	int nCount = m_listItem.GetCount();
	for(int i = 0; i < nCount; i++)
	{
		PadItem &item = m_listItem.GetAt(i);
		if(item.rcItem.PtInRect(point))
		{
    		if(m_nHovered != i)
			{
				if(m_nHovered >= 0)
				{
					PadItem &hoverItem = m_listItem.GetAt(m_nHovered);
					CRect rcHoverItem = hoverItem.rcItem;
					InvalidateUIRect(rcHoverItem);
				}

				m_nHovered = i;
				InvalidateUIRect(item.rcItem);
			}
		}
		else
		{
			if(m_nHovered == i)
			{
				m_nHovered = -1;
				PadItem &item = m_listItem.GetAt(i);
				CRect rcItem = item.rcItem;
				InvalidateUIRect(rcItem);
			}
		}
	}
	return TRUE;
}

BOOL CUIPad::OnMouseOut()
{
	if(m_nHovered >= 0)
	{
		PadItem &hoverItem = m_listItem.GetAt(m_nHovered);
		CRect rcHoverItem = hoverItem.rcItem;
		InvalidateUIRect(rcHoverItem);
		m_nHovered = -1;
	}
	return TRUE;
}